/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package posturecorrectionsystem.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/**
 *
 * @author ahmet
 */
public class WorkBreakAlarmSystem {
    
    private Timer warnForBreakTimer;
    private Timer warnForWorkTimer;
    
    private String settingsFileExtension;
    private String settingsFilename;
    private RestrictionWindow restrictionWindow; 

    public WorkBreakAlarmSystem(String settingsFileExtensionArg, String settingsFilenameArg, RestrictionWindow restrictionWindow) {
        settingsFileExtension = settingsFileExtensionArg;
        settingsFilename = settingsFilenameArg;

        this.restrictionWindow = restrictionWindow;
    }

    public void popup(String message) {
        //Here we construct a popup window
        //and show on the computer screen
        final JDialog popupWindow = new JDialog();
        popupWindow.setTitle("Posture Correction Warning");
        popupWindow.setSize(300, 100);
        popupWindow.getContentPane().setLayout(new BorderLayout());

        JLabel warningLabel = new JLabel(message, JLabel.CENTER);

        JPanel jp = new JPanel(new FlowLayout());
        JButton closeButton = new JButton("Close");
        closeButton.setMnemonic('C');
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                popupWindow.dispose();
            }
        });
        jp.add(closeButton);

        popupWindow.getContentPane().add(warningLabel, BorderLayout.CENTER);
        popupWindow.getContentPane().add(jp, BorderLayout.SOUTH);

        popupWindow.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        popupWindow.setAlwaysOnTop(true);
        popupWindow.setVisible(true);
    }

    public void sound() {
        try {
            AudioPlayer.player.start(new AudioStream(new FileInputStream("ding.wav")));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void restriction(String message) {
        //fill the screen with a dialog
        restrictionWindow.activate(message, true, 30000);
    }


    private Properties getSettings() {
        Properties settings = new Properties();
        try {
            settings.load(new FileInputStream(settingsFilename));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            //if there is no settings file, user must create one
            new SettingsManager(settingsFileExtension, settingsFilename).setVisible(true);
        } catch (IOException ex2) {
            ex2.printStackTrace();
        }
        return settings;
    }

    public void setWarnForBreakTimer() {
        Properties settings = getSettings();
        int workInterval = Integer.parseInt(settings.getProperty("warnWorkInterval"));

        if (warnForWorkTimer != null) {
            warnForWorkTimer.cancel();
            warnForWorkTimer = null;
        }

        warnForBreakTimer = new Timer("Warn for Break Timer");
        warnForBreakTimer.schedule(new WarnForBreak(), workInterval); 
    }

    public void setWarnForWorkTimer() {
        Properties settings = getSettings();
        int breakInterval = Integer.parseInt(settings.getProperty("warnBreakInterval"));

        if (warnForBreakTimer != null) {
            warnForBreakTimer.cancel();
            warnForBreakTimer = null;
        }

        warnForWorkTimer = new Timer("Warn for Work Timer");
        warnForWorkTimer.schedule(new WarnForWork(), breakInterval); 
    }

    class WarnForBreak extends TimerTask {
        @Override
        public void run() {
            //warn the user to take a break
            Properties settings = getSettings();
            if (settings.getProperty("warnWorkPopup").equals("true")) {
                popup("Please take a break!");
            }

            if (settings.getProperty("warnWorkSound").equals("true")) {
                sound();
            }

            if (settings.getProperty("warnWorkRestriction").equals("true")) {
                restriction("Please take a break!");
            }
        }
    }

    class WarnForWork extends TimerTask {
        @Override
        public void run() {
            //warn the user to go back to work
            Properties settings = getSettings();
            if (settings.getProperty("warnBreakPopup").equals("true")) {
                popup("Please go back to work!");
            }

            if (settings.getProperty("warnBreakSound").equals("true")) {
                sound();
            }

            if (settings.getProperty("warnBreakRestriction").equals("true")) {
                restriction("Go back to work!");
            }
        }
    }
}
